package com.hearwego;


import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ToggleButton;

import com.utilities.Utilities;

public class ActivityCategorias extends Activity {
	Button inicio, filtrar;
	ToggleButton tgfreepass, tgopen, tgpoi, tgmuseo, tgbar, tgcompras, tghwg, tgrest;
	SharedPreferences preferences;
	SharedPreferences.Editor editor;
	boolean bool= false;
	
	
	@Override
	public void onResume() {
		super.onResume();
		Log.i("Onresume", "Onresume");
		
		Log.i("BOOLEAN", ""+preferences.getInt("tgbar", 0));
		
        tgbar.setChecked(getBoolVar(preferences.getInt("tgbar", 0)));
        
        tgcompras.setChecked(getBoolVar(preferences.getInt("tgcompras", 0)));
        
        tgfreepass.setChecked(getBoolVar(preferences.getInt("tgfreepass", 0)));
        
        tghwg.setChecked(getBoolVar(preferences.getInt("tghwg", 0)));

        tgmuseo.setChecked(getBoolVar(preferences.getInt("tgmuseo", 0)));
        
        tgopen.setChecked(getBoolVar(preferences.getInt("tgopen", 0)));
        
        tgpoi.setChecked(getBoolVar(preferences.getInt("tgpoi", 0)));
        
        tgrest.setChecked(getBoolVar(preferences.getInt("tgrest", 0)));
	}
	
	boolean getBoolVar(int pref){
		boolean bool = false;
		if(pref == 1){
			bool = true;
		}else{
			bool = false;
		}
		return bool;
	}
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_categorias);
		
		preferences= getSharedPreferences("Preferencias",Context.MODE_PRIVATE);
		editor = preferences.edit();
        
        
        inicio = (Button) findViewById(R.id.bt_inicio);
        filtrar = (Button) findViewById(R.id.bt_filtrar);
        
        tgbar = (ToggleButton) findViewById(R.id.tg_bar);
        tgcompras = (ToggleButton) findViewById(R.id.tg_compras);
        tgfreepass = (ToggleButton) findViewById(R.id.tg_entrada_libre);
        tghwg = (ToggleButton) findViewById(R.id.tg_hwg);
        tgmuseo = (ToggleButton) findViewById(R.id.tg_museo);
        tgopen = (ToggleButton) findViewById(R.id.tg_solo_abiertos);
        tgpoi = (ToggleButton) findViewById(R.id.tg_poi);
        tgrest = (ToggleButton) findViewById(R.id.tg_restaurante);
        

        
        tgbar.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				if(isChecked){
					editor.putInt("tgbar", 1).commit();
					Log.i("tgbar", ""+preferences.getInt("tgbar", 0));
				}else{
					editor.putInt("tgbar", 0).commit();
					Log.i("tgbar", ""+preferences.getInt("tgbar", 0));
				}
			}
		});
        tgcompras.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				if(isChecked){
					editor.putInt("tgcompras", 1).commit();
					Log.i("tgcompras", ""+preferences.getInt("tgcompras", 0));
				}else{
					editor.putInt("tgcompras", 0).commit();
					Log.i("tgcompras", ""+preferences.getInt("tgcompras", 0));
				}
			}
		});        
        tgfreepass.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				if(isChecked){
					editor.putInt("tgfreepass", 1).commit();
					Log.i("tgfreepass", ""+preferences.getInt("tgfreepass", 0));
				}else{
					editor.putInt("tgfreepass", 0).commit();
					Log.i("tgfreepass", ""+preferences.getInt("tgfreepass", 0));
				}
			}
		});        
        tghwg.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				if(isChecked){
					editor.putInt("tghwg", 1).commit();
					Log.i("tghwg", ""+preferences.getInt("tghwg", 0));

				}else{
					editor.putInt("tghwg", 0).commit();
					Log.i("tghwg", ""+preferences.getInt("tghwg", 0));

				}
			}
		});        
        tgmuseo.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				if(isChecked){
					editor.putInt("tgmuseo", 1).commit();
					Log.i("tgmuseo", ""+preferences.getInt("tgmuseo", 0));
				}else{
					editor.putInt("tgmuseo", 0).commit();
					Log.i("tgmuseo", ""+preferences.getInt("tgmuseo", 0));
				}
			}
		});        
        tgopen.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				if(isChecked){
					editor.putInt("tgopen", 1).commit();
					Log.i("tgopen", ""+preferences.getInt("tgopen", 0));
				}else{
					editor.putInt("tgopen", 0).commit();
					Log.i("tgopen", ""+preferences.getInt("tgopen", 0));
				}
			}
		});        
        tgpoi.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				if(isChecked){
					editor.putInt("tgpoi", 1).commit();
					Log.i("tgpoi", ""+preferences.getInt("tgpoi", 0));
				}else{
					editor.putInt("tgpoi", 0).commit();
					Log.i("tgpoi", ""+preferences.getInt("tgpoi", 0));
				}
			}
		});        
        tgrest.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				if(isChecked){
					editor.putInt("tgrest", 1).commit();
					Log.i("tgrest", ""+preferences.getInt("tgrest", 0));
				}else{
					editor.putInt("tgrest", 0).commit();
					Log.i("tgrest", ""+preferences.getInt("tgrest", 0));
				}
			}
		});
        
        inicio.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				onBackPressed();
			}
		});
        
        filtrar.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				editor.putString("actualquery", getQuery()).commit();
				Intent intent_mapa = new Intent(ActivityCategorias.this, ActivityResultados.class);
				startActivity(intent_mapa);
//	            mActivity.pushFragments(Utilities.TAB_A, new TabListResultados(),true,true);

				//abrir lista
			}
		});
		
		
	}
	
	protected String getQuery() {
		int type = 0;
		String query = null;
		if((preferences.getInt("tgfreepass", 0) == 1)&&((preferences.getInt("tgopen", 0) == 0))&&(( (preferences.getInt("tgpoi", 0) == 0) && (preferences.getInt("tgmuseo", 0) == 0) && (preferences.getInt("tgrest", 0) == 0) && 
				(preferences.getInt("tgbar", 0) == 0) && (preferences.getInt("tgcompras", 0) == 0) && (preferences.getInt("tghwg", 0) == 0)))){
			type = 1; //solo entrada libre
			Log.i("solo entrada libre", "solo entrada libre");
		}
		if( (preferences.getInt("tgfreepass", 0) == 1) && (preferences.getInt("tgopen", 0) == 1)&&(( (preferences.getInt("tgpoi", 0) == 0) && (preferences.getInt("tgmuseo", 0) == 0) && (preferences.getInt("tgrest", 0) == 0) && 
				(preferences.getInt("tgbar", 0) == 0) && (preferences.getInt("tgcompras", 0) == 0) && (preferences.getInt("tghwg", 0) == 0)))){
			type = 2; //solo entrada libre y abiertos
			Log.i("solo entrada libre y abiertos", "solo entrada libre y abiertos");
		}
		if((preferences.getInt("tgopen", 0) == 1)&&((preferences.getInt("tgfreepass", 0) == 0))&&(( (preferences.getInt("tgpoi", 0) == 0) && (preferences.getInt("tgmuseo", 0) == 0) && (preferences.getInt("tgrest", 0) == 0) && 
				(preferences.getInt("tgbar", 0) == 0) && (preferences.getInt("tgcompras", 0) == 0) && (preferences.getInt("tghwg", 0) == 0)))){
			type = 3; //solo abiertos
			Log.i("solo abiertos", "solo abiertos");
		}
		if( ((preferences.getInt("tgpoi", 0) == 1) || (preferences.getInt("tgmuseo", 0) == 1) || (preferences.getInt("tgrest", 0) == 1) || 
				(preferences.getInt("tgbar", 0) == 1) || (preferences.getInt("tgcompras", 0) == 1) || (preferences.getInt("tghwg", 0) == 1) )&&
				((preferences.getInt("tgopen", 0) == 0)&&((preferences.getInt("tgfreepass", 0) == 0)))){
			type = 4; //hay categorias seleccionadas
			Log.i("hay categorias seleccionadas", "hay categorias seleccionadas");
		}
		if( ( (preferences.getInt("tgpoi", 0) == 1) || (preferences.getInt("tgmuseo", 0) == 1) || (preferences.getInt("tgrest", 0) == 1) || 
				(preferences.getInt("tgbar", 0) == 1) || (preferences.getInt("tgcompras", 0) == 1) || (preferences.getInt("tghwg", 0) == 1) )  &&
				(preferences.getInt("tgopen", 0) == 0)&&((preferences.getInt("tgfreepass", 0) == 1))){
			Log.i("hay categorias seleccionadas y entrada libre", "hay categorias seleccionadas y entrada libre");
			type = 5; //hay categorias seleccionadas y entrada libre
		}
		if( ((preferences.getInt("tgpoi", 0) == 1) || (preferences.getInt("tgmuseo", 0) == 1) || (preferences.getInt("tgrest", 0) == 1) || 
				(preferences.getInt("tgbar", 0) == 1) || (preferences.getInt("tgcompras", 0) == 1) || (preferences.getInt("tghwg", 0) == 1) )  &&
				((preferences.getInt("tgfreepass", 0) == 1) && ( preferences.getInt("tgopen", 0) == 1) )){
			Log.i("hay categorias seleccionadas, entrada libre y abiertos", "hay categorias seleccionadas, entrada libre y abiertos");
			type = 6; //hay categorias seleccionadas, entrada libre y abiertos
		}
		if( ( (preferences.getInt("tgpoi", 0) == 1) || (preferences.getInt("tgmuseo", 0) == 1) || (preferences.getInt("tgrest", 0) == 1) || 
				(preferences.getInt("tgbar", 0) == 1) || (preferences.getInt("tgcompras", 0) == 1) || (preferences.getInt("tghwg", 0) == 1) )  &&
				(preferences.getInt("tgopen", 0) == 1)&&((preferences.getInt("tgfreepass", 0) == 0))){
			Log.i("hay categorias seleccionadas y abiertos", "hay categorias seleccionadas y abiertos");
			type = 7; //hay categorias seleccionadas y abiertos
		}
		switch (type) {
		case 1:
			query = Utilities.MainUrl+"select+*+from+pois+where+poi_entradalibre=1&BaseDatos=hwg";
			Log.i("QUERY", ""+query);
			break;
		case 2:
			query = Utilities.MainUrl+"select+*+from+pois+where+poi_entradalibre=1and+(select+count(*)+" +
					"from+pois_horarios+where+pois_horarios.poi_idr+=pois.poi_idr+and+hor_dia+=+case+when+" +
					"date_part('dow',+now())+=+0+then+7+else+date_part('dow',+now())+end+and+cast+" +
					"(hor_abre+as+time)+%3C=+cast+(now()+as+time)+and+cast+(hor_cierra+as+time)+%3E=+cast+" +
					"(now()+as+time))%3E0&BaseDatos=hwg";
			Log.i("QUERY", ""+query);
			break;
		case 3:
			query = Utilities.MainUrl+"select+*+from+pois+where+(select+count(*)+from+pois_horarios+where+" +
					"pois_horarios.poi_idr+=pois.poi_idr+and+hor_dia+=+case+when+date_part('dow',+now())+=+0+then+7+" +
					"else+date_part('dow',+now())+end+and+cast+(hor_abre+as+time)+%3C=+cast+(now()+as+time)+and+" +
					"cast+(hor_cierra+as+time)+%3E=+cast+(now()+as+time))%3E0&BaseDatos=hwg";
			Log.i("QUERY", ""+query);
			break;
		case 4:
			
			query = Utilities.MainUrl+"select+*+from+pois+where+poi_idr+in(select+poi_idr+from+pois_categorias+" +
					"where+cat_cve+in("+getSelectedCategories()+"))&BaseDatos=hwg";
			Log.i("QUERY", ""+query);
			break;
		case 5:
			query = Utilities.MainUrl+"select+*+from+pois+where+poi_entradalibre=1+and+poi_idr+in(select+poi_idr+from+" +
					"pois_categorias+where+cat_cve+in("+getSelectedCategories()+"))&BaseDatos=hwg";
			Log.i("QUERY", ""+query);
			break;
		case 6:
			query = Utilities.MainUrl+"select+*+from+pois+where+poi_entradalibre=1+and+poi_idr+in(select+poi_idr+from+" +
					"pois_categorias+where+cat_cve+in("+getSelectedCategories()+"))+and+(select+count(*)+from+pois_horarios+where+" +
					"pois_horarios.poi_idr+=pois.poi_idr+and+hor_dia+=+case+when+date_part('dow',+now())+=+0+then+7+else+" +
					"date_part('dow',+now())+end+and+cast+(hor_abre+as+time)+%3C=+cast+(now()+as+time)+and+" +
					"cast+(hor_cierra+as+time)+%3E=+cast+(now()+as+time))%3E0&BaseDatos=hwg";
			Log.i("QUERY", ""+query);
			break;
		case 7:
			query = Utilities.MainUrl+"select+*+from+pois+where+poi_idr+in(select+poi_idr+from+pois_categorias+where+" +
					"cat_cve+in("+getSelectedCategories()+"))+and+poi_entradalibre=1and+(select+count(*)+from+pois_horarios+where+" +
					"pois_horarios.poi_idr+=pois.poi_idr+and+hor_dia+=+case+when+date_part('dow',+now())+=+0+then+7+else+" +
					"date_part('dow',+now())+end+and+cast+(hor_abre+as+time)+%3C=+cast+(now()+as+time)+and+" +
					"cast+(hor_cierra+as+time)+%3E=+cast+(now()+as+time))%3E0&BaseDatos=hwg";
			Log.i("QUERY", ""+query);
			break;
		default:
			break;
		}
		return query;
	}
	
	private String getSelectedCategories() {
		StringBuilder sb = new StringBuilder();
		if(preferences.getInt("tgpoi", 0) == 1){
			sb.append("7,");
		}
		if(preferences.getInt("tgmuseo", 0) == 1){
			sb.append("2,");
		}
		if(preferences.getInt("tgrest", 0) == 1){
			sb.append("3,");
		}
		if(preferences.getInt("tgbar", 0) == 1){
			sb.append("4,");
		}
		if(preferences.getInt("tgcompras", 0) == 1){
			sb.append("5,");
		}
		if(preferences.getInt("tghwg", 0) == 1){
			sb.append("6,");
		}
		Log.i("Selected Categories", ""+method(sb.toString()));
		return method(sb.toString());
	}
	
	public String method(String str) {
		 if (str.charAt(str.length()-1)==',')
		 {
		  str = str.substring(0, str.length()-1);
		  return str;
		 }
		 else
		 {
		  return str;
		 }
		}

//	@Override
//	public boolean onCreateOptionsMenu(Menu menu) {
//		// Inflate the menu; this adds items to the action bar if it is present.
//		getMenuInflater().inflate(R.menu.cate, menu);
//		return true;
//	}

}
